package adata;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import model.ApplicationModel;
import model.ReviewerModel;
import model.SubmissionModel;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
import forms.ReviewerForm;

public class SubmitReviewAction extends Action{
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, 
		    HttpServletRequest request, HttpServletResponse response) 
		    throws DbException{
		
//		RowSet crsCountReviewers = ReviewerModel.getReviewerCount();
//		request.setAttribute("crsCountReviewers", crsCountReviewers);
		
		ReviewerForm rf = (ReviewerForm) form;
		int grade = rf.getGrade();
		String comment = rf.getComment();
		
		int appid = (Integer) (request.getSession().getAttribute("currentAppID"));


		String reviewerName = request.getUserPrincipal().getName();
		
		SubmissionModel.submitGrade(appid, grade, comment, reviewerName);
		
		boolean lessThanThree = SubmissionModel.getReviewerCount(appid);

		RowSet otherReviewers = ReviewerModel.getOtherReviewers(reviewerName);
		request.setAttribute("otherReviewers", otherReviewers);

		
		if (lessThanThree){
			return mapping.findForward("addreviewer");
		}else{
		return mapping.findForward("success");
		}
	}
}